package com.car.carcontrol;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.car.carcontrol.base.BaseFragment;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

//净化器
public class Fragment_Third extends BaseFragment {
    public static String TAG = "3";

    private FrameLayout v;
    private Context context;

    private ImageView iv_clear_device;  //净化器

    GifImageView iv_route;  //gif控件
    GifDrawable gifDrawable;  //gifDrawable

    Object[] gifObj;    //用于界面离开后gif还能动

    private long loopTime = 1000;  //时间显示间隔   默认100毫秒
    private boolean isThread = true;
    Thread thread;

    //gif——gif动图: 控件类型、控件大小位置、gif动图资源id、能否点击；
    Object[] allParam = new Object[]{
            new Object[]{"iv", new Rect(373*width/1280-width_d, 151*height/480-height_d, 777*width/1280, 285*height/480), R.drawable.border, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"iv", new Rect(667*width/1280-width_d, 216*height/480-height_d, 165*width/1280, 158*height/480), R.drawable.clear_device, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"iv", new Rect(540*width/1280-width_d, 330*height/480-height_d, 85*width/1280, 85*height/480), R.drawable.open, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"iv", new Rect(870*width/1280-width_d, 330*height/480-height_d, 85*width/1280, 85*height/480), R.drawable.close, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"gif", new Rect(667*width/1280-width_d, 216*height/480-height_d, 165*width/1280, 158*height/480), R.drawable.clear_device_gif, false},

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = new FrameLayout(getActivity());

        context = v.getContext();
//        activity = getActivity();

        Log.e("fragment_Third===0", v+"==="+width_d+"==="+height_d);


        initView();

        isThread = true;
        loopRead();

        return v;
    }

    @Override
    public void onDestroy() {
        Log.e("f3===onDestroy", "===");

        isThread=false;

        if(thread!=null){
            thread.interrupt();
        }

        super.onDestroy();
    }

    private void initView(){
        for (int i=0; i<allParam.length; i++){
            Object[] obj = (Object[])allParam[i];
            Rect rect = (Rect)obj[1];
            String type = (String)obj[0];

            if("tv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = (int)obj[3];
                int textColor = (int)obj[4];
                TextView tv = new TextView(context);
                tv.setText(name);
                tv.setTextSize(textSize);
                tv.setTextColor(textColor);
                v.addView(tv, params);
            }else if("gif".equals(type)){   //GifImageView 加载gif


                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                gifObj = obj;
                int imgSrc = (int)obj[2];
                iv_route = new GifImageView(context);
                iv_route.setImageResource(imgSrc);
                iv_route.setScaleType(ImageView.ScaleType.FIT_XY);
                gifDrawable = (GifDrawable) iv_route.getDrawable();

                boolean isOpen = (boolean)obj[3];
                if(isOpen){
                    iv_clear_device.setVisibility(View.GONE);
                    iv_route.setVisibility(View.VISIBLE);
                    gifDrawable.start();
                }else{
                    iv_clear_device.setVisibility(View.VISIBLE);
                    iv_route.setVisibility(View.GONE);
                    gifDrawable.stop();
                }



                v.addView(iv_route, params);

            }else if("iv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
                ImageView iv = new ImageView(context);
                iv.setImageResource(imgSrc);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                v.addView(iv, params);

                if(imgSrc==R.drawable.clear_device){
                    iv_clear_device = iv;
                }



                boolean isCanClick = (boolean)obj[3];

//                Log.e("fragment_Third===can", imgSrc+"==="+isCanClick);

                if(isCanClick){
                    iv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            Log.e("onTouch===", v+"==="+event.getAction());

                            int imgId = (int)obj[2];
                            byte[] bytes = (byte[])obj[4];

                            if(event.getAction()==1){

                                iv.setImageResource(imgId);

                                if(imgId==R.drawable.open){
                                    iv_clear_device.setVisibility(View.GONE);
                                    iv_route.setVisibility(View.VISIBLE);

                                    gifDrawable.start();
//                                    ((Object[])allParam[3])[3] = true;
                                    gifObj[3] = true;

                                }else if(imgId==R.drawable.close){
                                    iv_clear_device.setVisibility(View.VISIBLE);
                                    iv_route.setVisibility(View.GONE);

                                    gifDrawable.stop();
//                                    ((Object[])allParam[3])[3] = false;
                                    gifObj[3] = false;
                                }


                            }else{
                                if(imgId==R.drawable.open){
                                    iv.setImageResource(R.drawable.open2);
                                }else if(imgId==R.drawable.close){
                                    iv.setImageResource(R.drawable.close2);
                                }
                            }

//                            write(bytes); //TODO

                            return false;
                        }
                    });

                    iv.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
//                        ToastUtil.showMessageApp(context, imgSrc+"==="+obj[2]);

                            int imgId = (int)obj[2];
                            byte[] bytes = (byte[])obj[4];

                            Log.e("fragment_Third===iv", imgId+"==="+bytes.length+"==="+bytes);

//                            if(imgId==R.drawable.open){
//                                iv.setImageResource(R.drawable.open2);
//                                obj[2] = R.drawable.open2;
//
//                                gifDrawable.start();
//                            }else if(imgId==R.drawable.open2){
//                                iv.setImageResource(R.drawable.open);
//                                obj[2] = R.drawable.open;
//                            }else if(imgId==R.drawable.close){
//                                iv.setImageResource(R.drawable.close2);
//                                obj[2] = R.drawable.close2;
//
//                                gifDrawable.stop();
//                            }else if(imgId==R.drawable.close2){
//                                iv.setImageResource(R.drawable.close);
//                                obj[2] = R.drawable.close;
//                            }

                            /* 串口发送字节 */
                            write(bytes); //TODO


                        }
                    });
                }

            }
        }


    }

    private void loopRead(){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isThread){
                    try {
                        Thread.sleep(loopTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if(MainActivity.curPos==2){
                        m_myHandler.sendEmptyMessage(1);
                    }


                }

            }
        });
        thread.start();
    }

    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;

                case 1:
//                    read(cmd_needle); //TODO

                    Log.e("f3===", "===");

                    break;


                default:
                    break;
            }
            return false;
        }
    });


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

    }

}

